package com.training.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.training.admin.dao.CourseDao;
import com.training.admin.dto.CourseRequest;
import com.training.admin.entity.course.Course;
import com.training.admin.service.CourseService;
/**
 * 
 * @author sjh
 *
 */
@Service("courseService")
public class CourseServiceImpl extends DataServiceImpl<Course> implements CourseService{

	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Dao
	private CourseDao courseDao;
	
	
	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.CourseService#create(com.training.admin.dto.CourseRequest)
	 */
	public void create(CourseRequest courseRequest) {
		
		if(!courseRequest.validate()) {
			
			throw BusinessException.newInstance("400", "申请参数错误"); 
		}
		 
		/* Course loadByCode = courseDao.loadByCode(courseRequest.getCode()); */
		
		 Course loadByName = courseDao.loadByName(courseRequest.getName());
		
		/*
		 * if(loadByCode !=null) {
		 * 
		 * throw BusinessException.newInstance("400", "课程编号已存在");
		 * 
		 * } else
		 */
		 if(loadByName != null) {
			
			throw BusinessException.newInstance("400", "课程名称已存在");
	
		}
		
		 	Course course = courseRequest.generateCourse();
		
		 	genericDaoSupport.save(course.initliaze());
	
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.CourseService#edit(Long, Course)
	 */
	public void edit(Long id, Course course) {
		
		Course cour = genericDaoSupport.load(Course.class, id);
		
		genericDaoSupport.update(cour.edit(course));
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.CourseService#delete(Long)
	 */
	public void delete(Long id) {
		
		Course course = genericDaoSupport.load(Course.class, id);
		
		genericDaoSupport.update(course.delete());
	}


	/* (non-Javadoc)
	 * @see com.training.admin.service.CourseService#listByTrainer(java.lang.Long)
	 */
	@Override
	public List<Course> listByTrainer(Long trainerId) {
		return courseDao.listByTrainer(trainerId);
	}

}
